BOT_NAME = "crawler"

SPIDER_MODULES = ["src.spiders"]
NEWSPIDER_MODULE = "src.spiders"

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
DOWNLOAD_DELAY = 0
CONCURRENT_REQUESTS_PER_DOMAIN = 16
CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en",
}

# Enable or disable spider middlewares
SPIDER_MIDDLEWARES = {
    "src.middlewares.CrawlerSpiderMiddleware": 543,
}

# Enable or disable downloader middlewares
DOWNLOADER_MIDDLEWARES = {
    "src.middlewares.proxy_middleware.ProxyMiddleware": 100,
    "src.middlewares.useragent_middleware.UserAgentMiddleware": 200,
    "src.middlewares.rate_limit_middleware.RateLimitMiddleware": 300,
    "src.middlewares.retry_middleware.ExponentialBackoffRetryMiddleware": 400,
    "scrapy.downloadermiddlewares.retry.RetryMiddleware": None,  # Disable default retry middleware
}

# Enable or disable extensions
EXTENSIONS = {}

# Configure item pipelines
ITEM_PIPELINES = {
    "src.pipelines.ValidationPipeline": 100,
    "src.pipelines.MongoDBPipeline": 200,
    "src.pipelines.PostgreSQLPipeline": 300,
}

# Retry configuration
RETRY_ENABLED = True
RETRY_TIMES = 3
RETRY_HTTP_CODES = [500, 502, 503, 504, 408, 429]
RETRY_BACKOFF_BASE = 2
RETRY_BACKOFF_MAX = 60

# AutoThrottle extension settings
AUTOTHROTTLE_ENABLED = True
AUTOTHROTTLE_START_DELAY = 1
AUTOTHROTTLE_MAX_DELAY = 60
AUTOTHROTTLE_TARGET_CONCURRENCY = 4.0
AUTOTHROTTLE_DEBUG = False

# HTTP cache
HTTPCACHE_ENABLED = True
HTTPCACHE_EXPIRATION_SECS = 3600
HTTPCACHE_DIR = "httpcache"
HTTPCACHE_IGNORE_HTTP_CODES = [503, 504, 400, 403, 404]
HTTPCACHE_STORAGE = "scrapy.extensions.httpcache.FilesystemCacheStorage"

# Set settings whose default value is deprecated to a future-proof value
REQUEST_FINGERPRINTER_IMPLEMENTATION = "2.7"
TWISTED_REACTOR = "twisted.internet.asyncioreactor.AsyncioSelectorReactor"
FEED_EXPORT_ENCODING = "utf-8"

# Logging
LOG_LEVEL = "INFO"
LOG_FORMAT = "%(levelname)s %(asctime)s [%(name)s] %(message)s"

# Database connections (to be loaded from environment)
MONGODB_URI = "mongodb://mongodb:27017/"
MONGODB_DATABASE = "ai_writing"
MONGODB_COLLECTION = "crawled_data"

POSTGRES_HOST = "postgres"
POSTGRES_PORT = 5432
POSTGRES_DB = "ai_writing"
POSTGRES_USER = "admin"
POSTGRES_PASSWORD = "admin123"

REDIS_HOST = "redis"
REDIS_PORT = 6379
REDIS_DB = 0

# Proxy settings
PROXY_ENABLED = False
PROXY_LIST = []

# User agent pool
USER_AGENT_LIST = []